﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.BLL;
using He.Web.UI;
using System.Data;
using He.Common;

namespace He.Web.manage.category
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString();      //操作类型
        protected int id = 0;
        protected int category_id;                                    //类别ID
        private int channel_id;                                       //栏目ID
        public int parent_id;
        public string imgurl = "";
        public Model.channel channel;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
                channel_id = new He.BLL.category().GetModel(id).channel_id;
            }
            else
            {
                if (!int.TryParse(Request.QueryString["channel_id"], out channel_id))
                {
                    ShowParamError();
                    return;
                }
                if (!int.TryParse(Request.QueryString["parentId"], out this.parent_id))
                {
                    ShowParamError();
                    return;
                }
            }

            channel = new He.BLL.channel().GetModel(channel_id);

            if (!Page.IsPostBack)
            {
                ddlCategoryBind();
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    chkLoginLevel("editCategory" + channel.id);
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addCategory" + channel.id);
                }
            }

        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.category model = new BLL.category().GetModel(_id);
            this.ddlPid.SelectedValue = model.parent_id.ToString();
            this.txtTitle.Text = model.title;
            this.txtSubTitle.Text = model.sub_title;
            this.txtSortId.Text = model.sort_id.ToString();
            this.txtImgUrl.Text = model.img_url;
            this.txtLinkUrl.Text = model.link_url;
            this.txtSeoKeywords.Text = model.seo_keywords;
            this.txtSeoDescription.Text = model.seo_desc;

            this.ddlPid.Enabled = false;
            imgurl = model.img_url;

        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            int layer = 1;
            BLL.category bll = new BLL.category();
            Model.category model = new Model.category();
            model.title = txtTitle.Text.Trim();
            model.sub_title = txtSubTitle.Text.Trim();
            model.parent_id = int.Parse(this.ddlPid.SelectedValue);
            model.id_list = "";//待定
            model.layer = layer;//待定
            model.sort_id = Convert.ToInt32(txtSortId.Text.Trim());
            model.img_url = txtImgUrl.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.seo_title = "";
            model.seo_keywords = txtSeoKeywords.Text.Trim();
            model.seo_desc = txtSeoDescription.Text.Trim();
            model.content = "";
            model.add_time = DateTime.Now.ToString();
            model.channel_id = channel_id;
            //插入模块并获取本条记录ID
            int thisId = new BLL.category().Add(model);
            //获取新插入数据行的id_list
            if (model.parent_id > 0)
            {
                model.id_list = bll.GetModel(model.parent_id).id_list + thisId.ToString() + ",";//父类id_list加自身ID
                model.layer = bll.GetModel(model.parent_id).layer + 1;
            }
            else
            {
                model.id_list = "," + thisId.ToString() + ",";
                model.layer = 1;
            }
            model.id = thisId;
            //更新id_list及layer
            if (new BLL.category().Update(model))
            {
                SaveLogs(string.Format("[{0}管理]增加分类:{1}", channel.manage_title, model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            Model.category model = new BLL.category().GetModel(_id);
            model.title = txtTitle.Text.Trim();
            model.sub_title = txtSubTitle.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.img_url = txtImgUrl.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.seo_title = "";
            model.seo_keywords = txtSeoKeywords.Text.Trim();
            model.seo_desc = txtSeoDescription.Text.Trim();
            model.add_time = DateTime.Now.ToString();

            if (new BLL.category().Update(model))
            {
                SaveLogs(string.Format("[{0}管理]修改分类:{1}", channel.manage_title, model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 绑定分类=================================
        private void ddlCategoryBind()
        {
            BLL.category bll = new BLL.category();
            DataTable dt = bll.GetList(0, this.channel_id);
            this.ddlPid.Items.Clear();
            this.ddlPid.Items.Add(new ListItem("作为顶级分类", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string id = dr["id"].ToString();
                int layer = int.Parse(dr["layer"].ToString());
                string title = dr["title"].ToString().Trim();
                if (layer < this.channel.max_layer)
                {
                    title = StringHelper.Repeat("　", layer) + "├ " + title;
                    this.ddlPid.Items.Add(new ListItem(title, id));
                }
            }
            this.ddlPid.SelectedValue = parent_id.ToString();
        }
        #endregion

        //保存修改
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", "list.aspx?channel_id=" + channel_id, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", "list.aspx?channel_id=" + channel_id, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "list.aspx?channel_id=" + channel_id, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", "list.aspx?channel_id=" + channel_id, "Success");
            }
        }
    }
}